package com.atguigu.cloud.controller;

import com.atguigu.cloud.entities.PayDTO;
import com.atguigu.cloud.resp.ResultData;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

/**
 * @author ViGrayThorn
 * @since 1.0.0
 * date 2025-09-02 08:52:32
 */
@RestController
@RequestMapping(value = "/consumer/")
@Tag(name = "订单微服务模块", description = "订单CRUD")
public class OrderController {

	public static final String PAYMENTS_URL = "http://cloud-payment-service";
	@Resource
	private RestTemplate restTemplate;

	@PostMapping(value = "pay/add")
	@Operation(summary = "新增订单", description = "新增订单方法,json串做参数")
	public ResultData addOrder(@RequestBody PayDTO payDTO){
		return restTemplate.postForObject(PAYMENTS_URL + "/pay/add", payDTO, ResultData.class);
	}

	@DeleteMapping(value = "pay/delete/{id}")
	@Operation(summary = "删除订单", description = "删除订单方法")
	public ResultData deleteOrder(@PathVariable("id") Integer id){
		ResponseEntity<ResultData> response = restTemplate.exchange(
			PAYMENTS_URL + "/pay/delete/{id}", 
			HttpMethod.DELETE, 
			null, 
			ResultData.class, 
			id
		);
		return response.getBody();
	}

	@PutMapping(value = "pay/update")
	@Operation(summary = "修改订单", description = "修改订单方法")
	public ResultData updateOrder(@RequestBody PayDTO payDTO){
		ResponseEntity<ResultData> response = restTemplate.exchange(
			PAYMENTS_URL + "/pay/update", 
			HttpMethod.PUT, 
			new HttpEntity<>(payDTO), 
			ResultData.class
		);
		return response.getBody();
	}

	@GetMapping(value = "pay/select/{id}")
	@Operation(summary = "按照ID查订单", description = "查询订单方法")
	public ResultData selectOrderById(@PathVariable("id") Integer id){
		return restTemplate.getForObject(PAYMENTS_URL + "/pay/select/{id}", ResultData.class, id);
	}

	@GetMapping(value = "pay/selectAll")
	@Operation(summary = "查询全部订单", description = "查询所有订单方法")
	public ResultData selectAllOrders(){
		return restTemplate.getForObject(PAYMENTS_URL + "/pay/selectAll", ResultData.class);
	}

	@GetMapping(value = "pay/get/info")
	private String getInfoByConsul()
	{
		return restTemplate.getForObject(PAYMENTS_URL + "/pay/get/info", String.class);
	}
}
